<template>
  <normal-layout page-index="3-1">
    <div>
      <el-input style="margin: 10px;width: 80%" v-model="searchName"></el-input>
      <el-button type="primary" @click="searchData">搜索员工</el-button>
      <el-button @click="handleNew">新增员工</el-button>
      <staff-table :staff-list="staffList" @modifyed="getData"></staff-table>
      <!--新增客户dialog-->
      <el-dialog
        title="录入员工"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
        <div>
          <el-form :model="addStaff" label-position="left" label-width="30%">
            <el-form-item label="姓名">
              <el-input v-model="addStaff.name" style="width: 90%"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio v-model="addStaff.sex" label="f">男</el-radio>
              <el-radio v-model="addStaff.sex" label="m">女</el-radio>
            </el-form-item>
            <el-form-item label="年龄">
              <el-input-number v-model="addStaff.age" style="width: 60%"></el-input-number>
            </el-form-item>
            <el-form-item label="联系电话">
              <el-input v-model="addStaff.tel" style="width: 90%"></el-input>
            </el-form-item>
            <el-form-item label="职位">
              <el-select v-model="addStaff.position" style="width: 90%">
                <el-option label="销售员工" value="销售员工"></el-option>
                <el-option label="销售经理" value="销售经理"></el-option>
                <el-option label="实习生" value="实习生"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="newStaff">确 定</el-button>
  </span>
      </el-dialog>
    </div>
  </normal-layout>
</template>

<script>
  import NormalLayout from "../../layouts/NormalLayout";
  import StaffTable from "../../components/Management/StaffTable";
  export default {
    name: "index",
    components: {StaffTable, NormalLayout},
    data(){
      return{
        searchName:'',
        staffList: [],
        dialogVisible:false,
        addStaff:{
          name:'',
          sex:'m',
          age:'18',
          tel:'',
          position:'',
          leader:''
        }
      }
    },
    mounted() {
      this.getData()
    },
    methods:{
      handleNew(){
        this.dialogVisible=true;
      },
      handleClose(){
        this.dialogVisible=false;
      },
      async getData(){
        const ip = await this.$axios.get('/get_staff_table');
        this.staffList=ip.data;
        console.log(ip);
        console.log(this.staffList);
      },
      async searchData(){
        let mydata={
          searchName:this.searchName
        }
        this.$axios.post('/get_staff_table',mydata).then((response) => {
          console.log(response.data)
          this.staffList=response.data;
        })
      },
      async newStaff(){
        console.log("fuck")
        let mydata=this.addStaff;
        console.log(mydata)
        const ip = await this.$axios.$post('/new_staff',mydata);
        if (ip==='1'){
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          });
          this.handleClose();
        }else {
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          });
        }
        this.getData();
      },
    }
  }
</script>

<style scoped>

</style>
